from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional

class PermissionBase(BaseModel):
    # 必填字段
    perm_code: str = Field(..., description="权限编码", max_length=100)
    perm_name: str = Field(..., description="权限名称", max_length=100)
    perm_type: int = Field(..., description="权限类型：1-菜单 2-按钮 3-接口")
    
    # 可选字段
    parent_id: int = Field(0, description="父权限ID")
    resource_path: Optional[str] = Field(None, description="资源路径", max_length=200)
    status: int = Field(1, description="状态：0-禁用 1-启用")
    sort_order: int = Field(0, description="排序")

class PermissionCreate(PermissionBase):
    """创建权限时使用的模型"""
    
    class Config:
        from_attributes = True

class PermissionUpdate(BaseModel):
    """更新权限时使用的模型"""
    perm_code: Optional[str] = None
    perm_name: Optional[str] = None
    perm_type: Optional[int] = None
    parent_id: Optional[int] = None
    resource_path: Optional[str] = None
    status: Optional[int] = None
    sort_order: Optional[int] = None
    
    class Config:
        from_attributes = True

class Permission(PermissionBase):
    id: int
    create_time: Optional[datetime] = Field(None, description="创建时间")
    update_time: Optional[datetime] = Field(None, description="更新时间")
    
    class Config:
        from_attributes = True